//
//  QInputMoneyVC.m
//  QoilPublic
//
//  Created by 杨凯 on 16/8/16.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QInputMoneyVC.h"
#import "QCounterView.h"
@interface QInputMoneyVC ()

@end

@implementation QInputMoneyVC{

    UILabel *_resultLabel;
    NSMutableString * c;//计算器有删除数字功能，c来接收数字的删除结果
    NSString *result;//计算（+ - * /）的结果
    NSArray *_titleArr;
    NSString *newStr;//新值
    NSString *oldStr;//旧值
    NSString *fhStr;//符号
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"输入金额";
    oldStr = @"";
    newStr = @"";
    _titleArr = @[@"+",@"-",@"×",@"÷",@"1",@"2",@"3",@"",@"4",@"5",@"6",@"AC",@"7",@"8",@"9",@"=",@".",@"0",@"",@""];
    WS(wSelf);
    self.automaticallyAdjustsScrollViewInsets = NO;
    int height;
    if(iphone6Plus_5_5){
        height = 450;
    }else{
        height = 350;
    }

    
    QCounterView *cview = [[QCounterView alloc] initWithFrame:CGRectMake(0,kScreenHeight-height, kScreenWidth, height) andBlock:^(NSInteger index) {
        [wSelf counterAction:index];
    }];
    [self.view addSubview:cview];
    
    UIView *whiteView = [[UIView alloc] init];
    whiteView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:whiteView];
    [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wSelf.view.mas_top).offset(kNavHeight+kStatusHeight);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(0);
        make.bottom.equalTo(cview.mas_top).offset(0);
    }];
    
    _resultLabel = [[UILabel alloc] init];
    _resultLabel.text = @"0";
    _resultLabel.textColor = [UIColor blackColor];
    _resultLabel.textAlignment = NSTextAlignmentRight;
    _resultLabel.font = [UIFont boldSystemFontOfSize:35.0];
    [whiteView addSubview:_resultLabel];
    [_resultLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(whiteView);
        make.height.equalTo(@60);
        make.left.equalTo(wSelf.view.mas_left).offset(0);
        make.right.equalTo(wSelf.view.mas_right).offset(-5);
    }];
}

- (void)counterAction:(NSInteger)index{

    switch (index) {
        case 4:
        case 5:
        case 6:
        case 8:
        case 9:
        case 10:
        case 12:
        case 13:
        case 14:
        case 16://点
        case 17://0
            CLog(@"拼接");
            if([_resultLabel.text isEqualToString:@"0"]){
                _resultLabel.text = @"";
            }
            if([fhStr isEqualToString:@"="]){//如果点击等号，恢复两个数的初始值
                newStr = @"";
                oldStr = @"";
            }
            if(oldStr.length > 0 && fhStr.length > 0){//表示有旧数据
                newStr = [newStr stringByAppendingString:_titleArr[index]];
                if(newStr.length == 2 && [newStr isEqualToString:@"00"]){
                    newStr = @"0";
                }
                if(newStr.length == 1 && [newStr isEqualToString:@"."]){
                    newStr = @"0.";
                }
                _resultLabel.text = newStr;
            }else{
                oldStr = [oldStr stringByAppendingString:_titleArr[index]];
                if(oldStr.length == 2 && [oldStr isEqualToString:@"00"]){
                    oldStr = @"0";
                }
                if(oldStr.length == 1 && [oldStr isEqualToString:@"."]){
                    oldStr = @"0.";
                }
                _resultLabel.text = oldStr;
            }
            [self ismoreD];
            break;
        case 11://清零
        {
            _resultLabel.text = @"0";
            newStr = @"";
            oldStr = @"";
            fhStr = @"";

        }
            break;
        case 7://清除
        {
            if([fhStr isEqualToString:@"="]){//如果点击等号，恢复两个数的初始值
                newStr = @"";
                oldStr = @"";
                _resultLabel.text=@"0";
            }

            c=[NSMutableString stringWithString:_resultLabel.text];
            [c  deleteCharactersInRange:NSMakeRange(c.length-1,1)];
            _resultLabel.text=c;
            if(oldStr.length > 0 && fhStr.length > 0){
                newStr = _resultLabel.text;
            }else{
                oldStr = _resultLabel.text;
            }
            
            if (_resultLabel.text.length==0) {
                _resultLabel.text=@"0";
                if(oldStr.length > 0 && fhStr.length > 0){
                    newStr = @"";
                }else{
                    oldStr = @"";
                    fhStr = @"";
                }
            }
        }
            break;
        case 0://加号
        case 1://减号
        case 2://乘号
        case 3://除号
        case 15://等号
            if([_titleArr[index] isEqualToString:@"+"] || [_titleArr[index] isEqualToString:@"-"] || [_titleArr[index] isEqualToString:@"×"] || [_titleArr[index] isEqualToString:@"÷"])
            {
                if([_resultLabel.text isEqualToString:@""]){
                    _resultLabel.text = @"0";
                }
                fhStr = _titleArr[index];
                if(fhStr.length > 0 && newStr.length > 0){//表示连加减乘除
                    _resultLabel.text = oldStr;
                    [self jjxc];
                    newStr = @"";
                }
                
                if(oldStr.length > 0){
                  _resultLabel.text = oldStr;
                }else{
                    _resultLabel.text = @"0";
                }
            }
            
            if([_titleArr[index] isEqualToString:@"="])
            {
                [self jjxc];
                newStr = @"";
                fhStr = _titleArr[index];
            }
        break;
        case 20://确定
        {
            NSString *resultStr = _resultLabel.text;
            if(resultStr.length == 0 || [resultStr isEqualToString:@"0"] || [resultStr isEqualToString:@"0."] || [resultStr isEqualToString:@"0.0"] || [resultStr isEqualToString:@"0.00"] ){
                [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"smile_image"] content:@"请输入有效金额" btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            }else{
                if(self.block){
                    self.block(resultStr);
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)jjxc{
    float after = [newStr floatValue];
    float before = [oldStr floatValue];
    if ([fhStr isEqualToString:@"+"]){//加法
        result=[NSString stringWithFormat:@"%.2f",[self jiafa:before b:after]];
        oldStr = result;
        _resultLabel.text = result;
    }
    if ([fhStr isEqualToString:@"-"]) {//减法
        result=[NSString stringWithFormat:@"%.2f",[self jianfa:before b:after]];
        oldStr = result;
        _resultLabel.text=result;
    }
    if ([fhStr isEqualToString:@"×"]) {//乘法
        result=[NSString stringWithFormat:@"%.2f",[self chengfa:before b:after]];
        oldStr = result;
        _resultLabel.text=result;
    }
    if ([fhStr isEqualToString:@"÷"]) {//除法
        result=[NSString stringWithFormat:@"%.2f",[self chufa:before b:after]];
        oldStr = result;
        _resultLabel.text=result;
    }
}
//判断有多个点，去掉一个点
- (void)ismoreD{
    
    NSArray *arr = [_resultLabel.text componentsSeparatedByString:@"."];
    if(arr.count){//长度的限制
        NSString *s = arr[0];
        if(s.length>10){
            c=[NSMutableString stringWithString:_resultLabel.text];
            [c  deleteCharactersInRange:NSMakeRange((c.length-1),1)];
            if(oldStr.length > 0 && fhStr.length > 0){
                newStr = c;
            }else{
                oldStr = c;
            }
            _resultLabel.text=c;
        }
    }
    
    NSInteger count=[arr count]-1;//去点
    if (count>1) {
        c=[NSMutableString stringWithString:_resultLabel.text];
        [c  deleteCharactersInRange:NSMakeRange((c.length-1),1)];
        if(oldStr.length > 0 && fhStr.length > 0){
            newStr = c;
        }else{
            oldStr = c;
        }
        _resultLabel.text=c;
    }
    if([_resultLabel.text containsString:@"."]){//保留小数点后面的
        NSArray *arrs = [_resultLabel.text componentsSeparatedByString:@"."];
        if(arrs.count){
            NSString *s = arrs[1];
            if(s.length > 2){
                c=[NSMutableString stringWithString:_resultLabel.text];
                [c  deleteCharactersInRange:NSMakeRange((c.length-1),1)];
                if(oldStr.length > 0 && fhStr.length > 0){
                    newStr = c;
                }else{
                    oldStr = c;
                }
                _resultLabel.text=c;
            }
        }
    }
}


-(float)jiafa:(float)a1 b:(float)b1{
    return a1+b1;
}

-(float)jianfa:(float)a1 b:(float)b1{
    return a1-b1;
}

-(float)chengfa:(float)a1 b:(float)b1{
    return a1*b1;
}

-(float)chufa:(float)a1 b:(float)b1{
    return a1/b1;
}

@end
